import javafx.scene.input.DataFormat;

import javax.swing.text.DateFormatter;
import javax.xml.crypto.Data;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Daytest {
    List<Node>list=new ArrayList<>();
    List<Point>stations=new ArrayList<>();
    Map<String,Point>stationmap=new HashMap<>();
    public void chushihua(){
//        for(int i=0;i<5;i++){
//            Point tem=GenerateTriangle.getPoint();
//            stations.add(tem);
//            stationmap.put("A"+i,tem);
//        }
        Point p1=new Point(5,6);
        Point p2=new Point(2,2);
        Point p3=new Point(8,2);
        Point p4=new Point(2,8);
        Point p5=new Point(8,8);
        stations.add(p1);
        stations.add(p2);
        stations.add(p3);
        stations.add(p4);
        stations.add(p5);
        stationmap.put("A0",p1);
        stationmap.put("A1",p2);
        stationmap.put("A2",p3);
        stationmap.put("A3",p4);
        stationmap.put("A4",p5);
    }
    public  void main() {
        int t=1000;
        int jian=200;
        chushihua();
        double sx=0;
        double sy=0;
        double ex=10;
        List<Point>zhengchan=new ArrayList<>();
        List<Point>toalist=new ArrayList<>();
        Random random=new Random();
        while (true)
        {
            long curtime=System.currentTimeMillis();
            while (true){
                try {
                    Thread.sleep(random.nextInt(jian));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int s=random.nextInt(stations.size());
                Point sta=stations.get(s);
                double cha=(double)(System.currentTimeMillis()-curtime)/t;
                Point now=new Point(sx+cha,sy+cha);
                list.add(new Node("A"+s,new Triangle(new Point(),new Point(),new Point()).length(stationmap.get("A"+s),now),System.currentTimeMillis()));
                if(System.currentTimeMillis()-curtime>=t)
                {
                    Set<Node>tem=this.filter(list);
                    list=new ArrayList<>();
                    long[]max=new long[]{Integer.MIN_VALUE,Integer.MAX_VALUE};
                    List<Node>ll=new ArrayList<>();
                    for (Node a : tem) {
                        ll.add(a);
                        max[0]=Math.max(max[0],a.time);
                        max[1]=Math.min(max[1],a.time);
                    }
                    Point pp=PositionByTriangle.getPoint(
                            stationmap.get(ll.get(0).stationId),
                            stationmap.get(ll.get(1).stationId),
                            stationmap.get(ll.get(2).stationId),
                            ll.get(0).distance,
                            ll.get(1).distance,
                            ll.get(2).distance);
                    sx+=((double)t/1000);
                    sy+=((double)t/1000);
                    zhengchan.add(new Point(sx,sy));
                    toalist.add(pp);
                    System.out.println(zhengchan.get(zhengchan.size()-1));
                    System.out.println(pp);
                    break;
                }
            }
            if(sx>=ex)break;
        }
        Double[][] line1=new Double[zhengchan.size()][];
        Double[][] line2=new Double[zhengchan.size()][];
        for(int i=0;i<zhengchan.size();i++) {
            line1[i] = new Double[]{zhengchan.get(i).getX(), zhengchan.get(i).getY()};
            line2[i] = new Double[]{toalist.get(i).getX(), toalist.get(i).getY()};
        }
        System.out.println(Arrays.deepToString(line1));
        System.out.println(Arrays.deepToString(line2));
    }
    public void detail(String str){
        str=str
                .replace("{","")
                .replace("}","")
                .replace("\"","");
        String[]arr=str.split(",");
        String stationId=arr[1].substring(10);
        double distance=Double.parseDouble(arr[1].substring(9));
        long time=System.currentTimeMillis();
        Node node=new Node(stationId,distance,time);
        list.add(node);
    }
    public Set<Node> filter(List<Node>list){
        Set<Node>result=null;
        int n=list.size();
        long min=Integer.MAX_VALUE;
        Map<String,Node>map=new HashMap<>();
        int L=0,R=0;
        while (true){
            //当窗口内不同基站小于三时，右指针移动一位
            if(map.size()<3)
            {
                if(R==n)break;
                map.put(list.get(R).stationId,list.get(R));
                R++;
            }
            //当窗口内不同基站等于三时，左指针移动一位
            else {
                String ID=list.get(L).stationId;
                if(map.get(ID)==list.get(L))map.remove(ID);
                L++;
            }
            //当窗口内不同基站等于三且左指针位于滑动窗口内最左边有效基站
            if(map.size()==3&&map.get(list.get(L).stationId)==list.get(L))
            {
                //当前滑动窗口内基站时间差小于最小时间差
                if(list.get(R-1).time-list.get(L).time<min)
                {
                    result=new HashSet<>();
                    //更新最小时间差
                    min=list.get(R-1).time-list.get(L).time;
                    for(String str:map.keySet())
                        result.add(map.get(str));
                }
            }
        }
        return result;
    }
}